
propsage.namespace("prestige.commissionPayable");

prestige.commissionPayable.index = (function($, ui){
	
	function initComponents() {
		$('#deletePayableItemDialog').dialog({
			'width': 420,
			'height': 220,
			'autoOpen': false,
			'resizable': false,
			'modal': true,
			'buttons': {
				'Cancel': onDialogCancel,
				'OK': {
					'text': 'Delete Payable Item',
					'class': 'blue',
					'click': onDeletePayableItemOK
				}
			},
			'open': onDeletePayableItemOpen
		});
	}
	
	function onDialogCancel()
	{
		$(this).dialog('close');
	}
	
	function initEventHandlers() 
	{ 
		$('#bulk-action-form').on('submit', onBulkActionFormSubmit);
	}
	
	function onDeletePayableItemOpen()
	{
		
	}
	
	function onDeletePayableItemOK()
	{
		var reason = $('#delete-reason').val();
		var selectedId = $('#payable-item-id').val();
		
		if(!reason){
			window.alert('Please enter a reason for deleting.');
			return false;
		}
		
		ui.doAjax('POST', baseUrl + '/commissionPayable/ajaxDelete', { 
			id : selectedId,
			delete_reason : reason
		}, onDeleteSuccess);
	}
	
	function onDeleteSuccess(response)
	{
		$('#deletePayableItemDialog').dialog('close');
		
		if (response.result == 'success'){
//			$('#commissions-payable-index-grid table.items input:checkbox[value="' + $('#payable-item-id').val() + '"]').closest('tr').remove();
			$.fn.yiiGridView.update("commissions-payable-index-grid");
			$('#delete-reason').val('');
			$('#payable-item-id').val('');
			ui.growl('Payable item deleted');
		} 
	}
	
	function onActionDeleteClick()
	{
		var selectedItem = $(this).closest('tr').find('input:checkbox[name="payable_item_id[]"]').val();
		$('#payable-item-id').val(selectedItem);
		$('#deletePayableItemDialog').dialog('open');
	}
	
	function onBulkActionFormSubmit(e) {
		var checkedPayableItems = $('[name="payable_item_id[]"]:checked');
		if (checkedPayableItems.size() == 0) {
			window.alert('No items selected');
			return false;
		}		
	}
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
	});
	
	return {
		'onActionDeleteClick': onActionDeleteClick
	}
    
}(jQuery, propsage.ui));